package com.kingsalert.kingsalert.analytics.data_sdk;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.kingsalert.kingsalert.analytics.data_sdk.logger.ASLger;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcAppsTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcBtyTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcClogTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcCntaTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcHadwTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcSsTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcStorTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASDevcWfiTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.ASFilterOdsTsk;
import com.kingsalert.kingsalert.analytics.data_sdk.task.OnDevcTskCmple;
import com.kingsalert.kingsalert.analytics.data_sdk.task.OnFilterOdsTskCmpt;
import com.kingsalert.kingsalert.analytics.data_sdk.util.ASUtil;

import java.util.concurrent.CompletableFuture;

public class ASSycManager {
    private final static String TAG = "ASSycManager";

    private ASSycManager() {
    }

    @SuppressLint("StaticFieldLeak")
    private static ASSycManager instance;

    public static ASSycManager getInstance() {
        if (instance == null) {
            synchronized (ASSycManager.class) {
                instance = new ASSycManager();
            }
        }
        return instance;
    }

    public Context mContext;

    private OnDevcTskCmple mOnDevcTskCmple;

    public void init(Context applicationContext) {
        mContext = applicationContext;
    }

    public void sycData(OnDevcTskCmple completeListener) {
        try {
            this.mOnDevcTskCmple = completeListener;

            // 创建任务列表
            CompletableFuture<String> device1Task = CompletableFuture.supplyAsync(new ASDevcBtyTsk(mContext, ASBuilder.T_BTY));
            CompletableFuture<String> device2Task = CompletableFuture.supplyAsync(new ASDevcStorTsk(mContext, ASBuilder.T_STO));
            CompletableFuture<String> device3Task = CompletableFuture.supplyAsync(new ASDevcAppsTsk(mContext, ASBuilder.T_APL));
            CompletableFuture<String> device4Task = CompletableFuture.supplyAsync(new ASDevcWfiTsk(mContext, ASBuilder.T_NTW));
            CompletableFuture<String> device5Task = CompletableFuture.supplyAsync(new ASDevcHadwTsk(mContext, ASBuilder.T_HDW));
            CompletableFuture<String> device6Task = CompletableFuture.supplyAsync(new ASDevcTsk(mContext, ASBuilder.T_DIE));
            CompletableFuture<String> device7Task = CompletableFuture.supplyAsync(new ASDevcSsTsk(mContext, ASBuilder.T_MAG));
            CompletableFuture<String> device8Task = CompletableFuture.supplyAsync(new ASDevcCntaTsk(mContext, ASBuilder.T_COTACT));
            CompletableFuture<String> device9Task = CompletableFuture.supplyAsync(new ASDevcClogTsk(mContext, ASBuilder.T_CAL));

            // 提交任务
            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    device1Task,
                    device2Task,
                    device3Task,
                    device4Task,
                    device5Task,
                    device6Task,
                    device7Task,
                    device8Task,
                    device9Task);

            // 在所有任务触发后回调，不一定要等结果完成！！！
            allOfFuture.thenRun(() -> {
                // 日记标识
                ASLger.d(TAG, "sycData: 在所有任务触发后回调");
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (mOnDevcTskCmple != null) {
                        mOnDevcTskCmple.onAllCompleted();
                    }
                });

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void filterOrders(OnFilterOdsTskCmpt completeListener) {
        CompletableFuture<String> filterOrdersTask = CompletableFuture.supplyAsync(new ASFilterOdsTsk(mContext));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(filterOrdersTask);
        allOfFuture.thenRunAsync(() -> {
            String result = ASUtil.safeString(filterOrdersTask.join()).trim();

            // 日记标识
            ASLger.d(TAG, "filterOrders: 在所有任务完成后回调");
            ASLger.d(TAG, "filterOrders result: " + result);

            // 主线程回调
            new Handler(Looper.getMainLooper()).post(() -> {
                if (completeListener != null) {
                    completeListener.onAllCompleted(result);
                }
            });
        });
    }
}
